﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestEntityFramework.DAOS;
using TestEntityFramework.Entities;
using TestEntityFramework.Interfaces;

namespace TestEntityFramework
{
    public class UnitTransaction : IUnitTransaction
    {
        private TestEntityFramework.Model.PGIngresoTareasEntities _dashboardContext;
        private IDAOIngresoHoras _daoPersonas;
        private IDAOTareas _daoTareas;
        private IDAOSubTareas _daoSubTareas;
        private bool _disposed = false;
        private RepositoryContext _repositoryContext;

        public UnitTransaction()
        {
            _dashboardContext = new TestEntityFramework.Model.PGIngresoTareasEntities();
        }

        public void SetContext(RepositoryContext repositoryContext)
        {
            _repositoryContext = repositoryContext;
        }

        public IDAOIngresoHoras DAOIngresoHoras
        {
            get
            {
                if (_daoPersonas == null)
                {
                    _daoPersonas = new DAOIngresoHoras(_dashboardContext, _repositoryContext);
                }

                return _daoPersonas;
            }
        }

        public IDAOTareas DAOTareas
        {
            get
            {
                if (_daoTareas == null)
                {
                    _daoTareas = new DAOTareas(_dashboardContext, _repositoryContext);
                }

                return _daoTareas;
            }
        }

        public IDAOSubTareas DAOSubTareas
        {
            get
            {
                if (_daoSubTareas == null)
                {
                    _daoSubTareas = new DAOSubTareas(_dashboardContext, _repositoryContext);
                }

                return _daoSubTareas;
            }
        }

        public void Save()
        {
            _dashboardContext.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _dashboardContext.Dispose();
                }
            }

            _disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
